package com.bidevalution.business.job;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.bidevalution.business.factory.StepJobJdbcFactory;
import com.bidevalution.business.function.FunctionCalculator;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.bidevalution.support.util.StringUtil;

/**
 * 计算环节审核结果（少数服从多数)；一个企业中，只要有一个项未通过，则该企业未通过
 * 
 * @author daizhifeng
 * @date 2019年9月30日 下午5:07:17
 * @version V2.0
 */
public class StepComputeResultJob implements StepJobJdbcFactory {
	private JdbcTemplateService jdbcTemplateService;

	@Override
	public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode,String expertId,String layouCode)
			throws Exception {
		String[] params = StringUtils.delimitedListToStringArray(beSectionStepJob.getJobContent(), "|");
		String[] columnCodes = StringUtils.delimitedListToStringArray(params[0],",");//c1,c2|c3 c1和c2表示计算的字段 c3表示审核结果字段

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("sectionStepId", beSectionStepJob.getSectionStepId());
		paramMap.put("submitCode", submitCode);
		List<String> columnValueList = new ArrayList<String>();
		List<Map<String, Object>> dataList;
		Map<String, Object> env;
		//获取评审环节的企业列表
		label:
		for (String enterpriseId : this.jdbcTemplateService.selectForList(
				"select DISTINCT enterprise_id from be_evalution_expert where section_step_id=:sectionStepId and is_del='0'",
				paramMap, String.class)) {
			paramMap.put("enterpriseId", enterpriseId);
			//获取评审数据
			dataList = this.jdbcTemplateService.selectForList(
					"select column_code,column_value from be_evalution_expert where section_step_id=:sectionStepId and is_del='0' and enterprise_id=:enterpriseId",
					paramMap);
			//将评审结果设置为通过
			paramMap.put("columnCode", params[1]);
			for (int i = 0; i < columnCodes.length; i++) {
				columnValueList = new ArrayList<String>();
				for (Map<String, Object> data : dataList) {
					if (columnCodes[i].equals(data.get("column_code"))) {
						columnValueList.add(StringUtil.toString(data.get("column_value")));
					}
				}
				env = new HashMap<String, Object>();
				env.put("columnValues", StringUtils.collectionToDelimitedString(columnValueList, ","));
				//1 表示通过，0 表示未通过
				if (!"1".equals(FunctionCalculator.computeAviator("maxFrequencyOfArray(columnValues)", env))) {
					
					this.jdbcTemplateService.update("update be_evalution_expert set column_value='0' where section_step_id=:sectionStepId and is_del='0' and enterprise_id=:enterpriseId and column_code=:columnCode", paramMap);
				     continue label;
				} else {
					this.jdbcTemplateService.update("update be_evalution_expert set column_value='1' where section_step_id=:sectionStepId and is_del='0' and enterprise_id=:enterpriseId and column_code=:columnCode", paramMap);
				}
			}

		}

		return true;
	}

	@Override
	public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
		this.jdbcTemplateService = jdbcTemplateService;
	}

}
